<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 短信管理模块
 */
class Sms extends Admin_Controller {
	
    function __construct()
    {
        parent::__construct();
        $this->load->model('sms_model');
    }
    //首页
    public function index()
    {
        $where = array();
        $where['type'] = 1;
        if($_GET['mobile']){
            $mobile = $_GET['mobile'];
            $where['account'] = $mobile;
        }
        
        if($_GET['time-start']){
            $where['create_time >='] = strtotime($_GET['time-start']);
        }
        if($_GET['time-end']){
            $where['create_time <='] = strtotime($_GET['time-end']." 23:59:59");
        }
        
        //总数
        $total = $this->sms_model->getCount($where);
        $navigation_position = 'outside';
        $records_per_page = 10;
        $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position,
            array('left', 'right')) ? $navigation_position : 'outside');
        //$this->xpage->reverse(true);
        $this->xpage->records($total);
        $this->xpage->records_per_page($records_per_page);
        $this->xpage->labels("<<", ">>");
        //记录
        $list = $this->sms_model->getList($where, (($this->xpage->get_page() - 1) * $records_per_page),
            $records_per_page);
        foreach ($list as $key=>$val){
            $list[$key]['send_type'] = $val['send_type'] == 1 ? '前台' : '后台';
            $list[$key]['create_time'] = date('Y-m-d H:i:s',$val['create_time']);
        }
        $this->template->assign('time_start', $_GET['time-start']);
        $this->template->assign('time_end', $_GET['time-end']);
        $this->template->assign('keyword', $_GET['mobile']);
        $this->template->assign('Total', $total);
        $this->template->assign('showpage', $this->xpage->render(true));
        $this->template->assign('list', $list);
        $this->template->assign('tt', 1);
        $this->template->load('sms/index');
    }
    
    function email_index(){
        $where = array();
        $where['type'] = 2;
        if($_GET['email']){
            $email = $_GET['email'];
            $where['account'] = $email;
        }
        
        if($_GET['time-start']){
            $where['create_time >='] = strtotime($_GET['time-start']);
        }
        if($_GET['time-end']){
            $where['create_time <='] = strtotime($_GET['time-end']." 23:59:59");
        }
        
        //总数
        $total = $this->sms_model->getCount($where);
        $navigation_position = 'outside';
        $records_per_page = 10;
        $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position,
            array('left', 'right')) ? $navigation_position : 'outside');
        //$this->xpage->reverse(true);
        $this->xpage->records($total);
        $this->xpage->records_per_page($records_per_page);
        $this->xpage->labels("<<", ">>");
        //记录
        $list = $this->sms_model->getList($where, (($this->xpage->get_page() - 1) * $records_per_page),
            $records_per_page);
        foreach ($list as $key=>$val){
            $list[$key]['send_type'] = $val['send_type'] == 1 ? '前台' : '后台';
            $list[$key]['create_time'] = date('Y-m-d H:i:s',$val['create_time']);
        }
        $this->template->assign('time_start', $_GET['time-start']);
        $this->template->assign('time_end', $_GET['time-end']);
        $this->template->assign('keyword', $_GET['email']);
        $this->template->assign('Total', $total);
        $this->template->assign('showpage', $this->xpage->render(true));
        $this->template->assign('list', $list);
        $this->template->assign('tt', 2);
        $this->template->assign('segment_p','index');
        $this->template->load('sms/email_index');
    }
    //短信发送
    function sendsms(){
        if($_POST){
            $mobile = trim($_POST['mobile']);//发送的手机号
            $content = trim($_POST['content']);//发送的消息
            
            if(!$mobile){
                $mssage['info']	  = '请输入手机号，最多支持2万个手机号';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
            if(!$content){
                $mssage['info']	  = '请输入短信内容';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
            
            if(count(explode(",", $mobile)) > 20000){
                $mssage['info']	  = '号码过多，请分多次发送';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
            $mobile = str_replace(array("\n","|",";"," "),",",$mobile);
            
            $data = array();
            $data['account'] = $mobile;
            $data['code'] = "";
            $data['content'] = $content;
            $data['status'] = 0;
            $data['type'] = 1;
            $data['send_type'] = 2;
            $data['create_date'] = date("Ymd");
            $data['create_time'] = time();
            $this->db->insert("tan_sms_content",$data);
            if($sms_id = $this->db->insert_id()){
                $this->load->library('My_spacei');
                $sms = new My_spacei();
                $result = $sms->sendSMS(iconv("UTF-8", "gbk", $mobile), iconv("UTF-8", "gbk", $content));
                $this->db->where("id",$sms_id)->update("tan_sms_content",array("status"=>$result));
                if(strstr($result, "success")){
                    $mssage['info']	  = '短信发送成功';
                    $mssage['status'] = 1;
                    $mssage['url']	  = '';
                    echo json_encode($mssage);exit;
                }else{
                    $mssage['info']	  = '发送失败，原因【'.$result.'】';
                    $mssage['status'] = 0;
                    $mssage['url']	  = '';
                    echo json_encode($mssage);exit;
                }
            }
        }
        
        $this->template->assign('segment_p','sendsms');
        $this->template->load('sms/sendsms');
    }
}
